/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.section;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.section.Section;
import com.unis.service.section.SectionService;

/**
 * @author Administrator (email:granzy@sohu.com)
 * @since 2013-02-25
 */
@Controller
@RequestMapping("/section")
public class SectionController {

    private SectionService sectionService;

    /****************************************
     * Insert a instace of Section
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertSection(@RequestBody Section section) throws Exception {
        section.setStatmentId("Section.insert");
        String status = "failure";
        sectionService.insert(section);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Section
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Section section) throws Exception {
        section.setStatmentId("Section.update");
        String status = "failure";
        sectionService.update(section);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Section
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Section section) throws Exception {
        section.setStatmentId("Section.delete");
        String status = "failure";
        sectionService.batchDelete(section);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Section
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Section section = new Section();
        section.setStatmentId("Section.queryAsList");
        BeanUtils.populate(section, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(section, pageMap);
        List<BaseModel> sectionList = sectionService.queryAsList(section);
        Map<String, Object> renderMap = GridUIMapper.render(section, sectionList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of Section
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Section section) throws Exception {
        section.setStatmentId("Section.queryAsObject");
        return sectionService.queryAsObject(section);
    }
    
    /****************************************
     * 新闻所属栏目选择框
     ****************************************/
    @RequestMapping("/select")
    @ResponseBody
    public List<?> getSelectData() throws Exception {
        List<Object> list = new ArrayList<Object>();

        Section sections = new Section();
        sections.setStatmentId("Section.queryAsList");
        sections.setPaging(false);
        List<BaseModel> sectionList = sectionService.queryAsList(sections);

        for (BaseModel baseModel : sectionList) {
            Map<String, Object> map = new HashMap<String, Object>();
            Section st = (Section) baseModel;
            map.put("sectionid", st.getSectionid());
            map.put("sectionname",st.getSectionname());
            list.add(map);
        }
        Map<String, Object> blankMap = new HashMap<String, Object>();
        blankMap.put("sectionid", "");
        blankMap.put("sectionname","&nbsp;");
        list.add(blankMap);
        return list;
    }

    @RequestMapping("/page")
    public String forword() {
        return "section/sectionList";
    }

    @Resource
    public void setSectionService(SectionService sectionService) {
        this.sectionService = sectionService;
    }
}
